<?php defined('SYSPATH') or die('No direct access allowed.');

class Controller_Admin_Service extends Controller_Admin_Template
{
    public function before()
    {
        $this->auto_render = false;
    }
    
    public function action_autocomplete()
    {       
        $response = array();
        
        $term = Arr::get($_GET, 'term', '');
        $table = Arr::get($_GET, 'table', '');
        $field = Arr::get($_GET, 'field', '');        
        
        if(in_array($table, array('products')))
        {
             
            try
            {
                $result = DB::query(Database::SELECT, "SELECT DISTINCT $field FROM $table WHERE $field LIKE '$term%'")
                                ->execute();
                
                foreach($result as $value)
                    $response[] = $value[$field];
        
            }
            catch(Exception $e) {}
        }               
        $this->response->body(json_encode($response));
    }
    
}